import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Category from '../views/Category.vue'
import Cart from '../views/Cart.vue' 
import User from '../views/User.vue'  
import Product from '../views/Product.vue'
import Login from '../views/Login.vue'
import store from '../store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
  },
  {
    path: '/Category',
    name: 'Category',
    component: Category,
  },
  {
    path: '/Cart',
    name: 'Cart',
    component: Cart,
  },
  {
    path: '/User',
    name: 'User',
    component: User,
  },
  {
    path:'/Product/:id/:month'+'.html',
    name:'Product',
    component: Product
  },
  {
    path:'/Login',
    name:'Login',
    component: Login
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//前置导航守卫
router.beforeEach((to, from, next) => {
  console.log(to)
  console.log(from)
  console.log(next)
  console.log(store)
  console.log(store.state.loginFig)
  if(store.state.loginFig){
    next()
  } else {
    if(to.path == '/cart'){
      console.log("未登录,请登录先")
      next('/login')
    } else {
      next()
    }
  }

})


export default router
